<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\AttractionManagement\Repository\Attraction_spot;

use Plugin\Attractions\AttractionManagement\Model\Attraction_spot\AttractionSpot;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 点位表 Repository类
 */
class AttractionSpotRepository extends IRepository
{
   public function __construct(
        protected readonly AttractionSpot $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // id
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 点位名称
        if (isset($params['name']) && !empty($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 位置Id
        if (isset($params['position_id']) && !empty($params['position_id'])) {
            $query->where('position_id', '=', $params['position_id']);
        }

        // 点位类型Id
        if (isset($params['type_id']) && !empty($params['type_id'])) {
            $query->where('type_id', '=', $params['type_id']);
        }

        // 点位介绍标题
        if (isset($params['intro_title']) && !empty($params['intro_title'])) {
            $query->where('intro_title', 'like', '%'.$params['intro_title'].'%');
        }

        // 点位介绍
        if (isset($params['intro']) && !empty($params['intro'])) {
            $query->where('intro', '=', $params['intro']);
        }

        // 所属景区Id
        if (isset($params['attraction_id']) && !empty($params['attraction_id'])) {
            $query->where('attraction_id', '=', $params['attraction_id']);
        }

        // 点位封面,关联attachment表
        if (isset($params['cover_id']) && !empty($params['cover_id'])) {
            $query->where('cover_id', '=', $params['cover_id']);
        }

        // 显示类型1:管理端显示2:管理游客双端显示
        if (isset($params['show_type']) && !empty($params['show_type'])) {
            $query->where('show_type', '=', $params['show_type']);
        }

        // 点位图标Id,关联attachment表
        if (isset($params['icon_id']) && !empty($params['icon_id'])) {
            $query->where('icon_id', '=', $params['icon_id']);
        }

        // 点位状态1:禁用2:正常
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 点位排序
        if (isset($params['sort']) && !empty($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 点位扩展字段
        if (isset($params['extension']) && !empty($params['extension'])) {
            $query->where('extension', '=', $params['extension']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}